import fetch from 'node-fetch'

export class ceshi extends plugin {
    constructor() {
        super({
            name: '欧&美卡状态',
            dsc: '查看欧卡2和美卡的TMP服务器状态',
            event: 'message',
            priority: 66666666666,
            rule: [
                {
                    reg: '^#欧卡状态$',
                    fnc: 'ou'
                }
            ]
        });
    }
async ou(e) {
                const jsonData = await fetch('https://api.truckersmp.com/v2/servers'); //获取数据
                const data = await jsonData.json(); // 将响应结果转换成 JSON 格式
                if (data?.response && Array.isArray(data.response) && data.response.length) {
                    // let serverList = "🚛 TruckersMP 服务器状态\n\n";
                    const messageArray = [];
                    // let serverList = [];
            
                    data.response.forEach(server => {
                        let serverList = `🏷️ 服务器名称: ${server.name}\n`;
                        serverList += `👥 当前人数: ${server.players}/${server.maxplayers || "N/A"}\n`;
                        serverList += `🎮 游戏类型: ${server.game || "Euro Truck Simulator 2"}\n`;
                        serverList += `🌍 地区: ${server.shortname}\n`;
                        serverList += `🚗 碰撞: ${server.collisions ? "✅" : "❌"}\n`;
                        serverList += `🧭 限速: ${server.speedlimiter ? "✅" : "❌"}\n`;
                        serverList += `💤 自动踢出挂机玩家: ${server.afkenabled ? "✅" : "❌"}\n`;
                        serverList += `🚓 允许玩家开警车: ${server.policecarsforplayers ? "✅" : "❌"}\n`;
                        serverList += `📶 在线状态: ${server.online ? "✅" : "❌"}\n`;
                        serverList += `🎉 活动: ${server.event ? "✅" : "❌"}`;
                        // serverList += "─".repeat(10) + "\n";
                        messageArray.push(serverList);
                    });
                    // await makeForwardMsg(e,serverList);
                    const forwardMsg = await makeForwardMsg(e, messageArray,`🚛 TruckersMP 服务器状态`);
                    await e.reply(forwardMsg); // 发送构造好的转发消息
                } else {
                    console.log('response 不存在或不是数组');
                }
    }
}
//输入data一个数组,元素是字符串,每一个元素都是一条消息.
function makeForwardMsg(e,msg =[],dec){
const forwardMsg=[]
if (dec)forwardMsg.push({message: dec})
for(const message of Array.isArray(msg)?msg :[msg])forwardMsg.push({message})
if(e?.group?.makeForwardMsg) return e.group.makeForwardMsg(forwardMsg)
else if(e?.friend?.makeForwardMsg) return e.friend.makeForwardMsg(forwardMsg)
else return Bot.makeForwardMsg(forwardMsg)
}
